import react from "@vitejs/plugin-react";
import path from "path";
import { defineConfig } from "vite";
import { viteMockServe } from "vite-plugin-mock";

export default defineConfig({
  plugins: [
    react(),
    viteMockServe({
      mockPath: "./mock/",
      // 开发环境开启mock
      enable: process.env.NODE_ENV === "development"
    })
  ],
  resolve: {
    alias: {
      // 使用@代替项目根目录下的src目录
      "@": path.resolve(__dirname, "src")
    }
  },
  // 开发服务器配置
  server: {
    port: 8080,
    open: true,
    cors: {
      origin: "*",
      methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
      preflightContinue: false,
      optionsSuccessStatus: 200
    },
    // 开发环境使用，直接转发到后端的开发环境接口地址
    proxy: {
      "/api": {
        target: "http://localhost:5000",
        changeOrigin: true
      }
    }
  }
});
